package zdream.rockchronicle.core.profile;

import java.util.function.Consumer;

import com.badlogic.gdx.Gdx;

import zdream.control.world.Ticker;
import zdream.rockchronicle.RockChronicle;

/**
 * <p>Level 进行时, 按暂停, 查看自己角色信息的界面.
 * 
 * <p>界面开启中、关闭中是两个过渡状态, 状态持续时间为 0.3s
 * </p>
 * 
 * @author Zdream
 * @since 0.0.1
 * @date
 *   2021-02-24 (last modified)
 */
public class ProfilePage {
	public final RockChronicle app;

	public ProfilePage(RockChronicle app) {
		this.app = app;
	}

	/* **********
	 *   状态   *
	 ********** */

	public static final byte STATUS_CLOSE = 0;
	public static final byte STATUS_OPENING = 1; // 过渡状态
	public static final byte STATUS_OPEN = 2;
	public static final byte STATUS_CLOSING = 3; // 过渡状态
	public byte status = STATUS_CLOSE;

	public boolean open() {
		if (status != STATUS_CLOSE) {
			return false;
		}
		doOpen();
		return true;
	}

	public boolean close() {
		if (status != STATUS_OPEN) {
			return false;
		}
		doClose();
		return true;
	}

	public boolean openOrClose() {
		if (status == STATUS_CLOSE) {
			doOpen();
			return true;
		} else if (status == STATUS_OPEN) {
			doClose();
			return true;
		}
		return false;
	}

	private void doOpen() {
		opDuration = 0;
		status = STATUS_OPENING;
	}

	private void doClose() {
		opDuration = 0;
		status = STATUS_CLOSING;
	}

	/* **********
	 *   执行   *
	 ********** */
	/**
	 * 距离上一次操作的时间差.
	 */
	long opDuration = -1;
	public static final long OP_DURATION = 250;

	public Consumer<ProfilePage> onProfileClosed = (p) -> {};

	public void tick(Ticker ticker) {
		if (status != STATUS_OPENING && status != STATUS_CLOSING) {
			return;
		}

		opDuration += (ticker.duration * 1000);
		if (opDuration >= OP_DURATION) {
			opDuration = 0;
			if (status == STATUS_OPENING) {
				status = STATUS_OPEN;
				Gdx.app.log(this.getClass().getSimpleName(), "打开了角色界面");
			} else {
				status = STATUS_CLOSE;
				Gdx.app.log(this.getClass().getSimpleName(), "关闭了角色界面");
				onProfileClosed.accept(this);
			}
		}
	}
}
